package com.njtoyo.taxi.admin.rest.controller.business.customer;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.customer.CustomerArrearsListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.customer.CustomerDetailPresenter;
import com.njtoyo.taxi.admin.rest.presenter.customer.CustomerListPresenter;
import com.njtoyo.taxi.admin.rest.presenter.customer.CustomerQueryLikeListPresenter;
import com.njtoyo.taxi.admin.rest.readfile.customer.CustomerQueryResult;
import com.njtoyo.taxi.admin.rest.wrapper.business.customer.*;
import com.njtoyo.taxi.admin.service.business.customer.CustomerService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
@Api(value = "乘客管理", tags = {"乘客管理"})
@RequestMapping("business/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping("")
    @ApiOperation(value = "乘客列表", notes = "用户列表,权限:customer_list")
    @PermissionScope(names = {"customer_list"})
    RestResult<Page<CustomerListPresenter>> index(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @Valid CustomerQueryWrapper wrapper) {
        if(StringUtils.isNotBlank(wrapper.getMobileNumber())){
            wrapper.setMobileNumber(wrapper.getMobileNumber().trim());
        }
        return customerService.getList(adminUser, wrapper);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "乘客详情", notes = "用户详情,权限:customer_detail")
    @PermissionScope(names = {"customer_detail"})
    RestResult<CustomerDetailPresenter> detailsById(@PathVariable("id") Long id) {
        return customerService.detailsById(id);
    }

    @PatchMapping("/{id}/mobileNumber")
    @ApiOperation(value = "换手机号码", notes = "换手机号,权限:customer_update_mobile_number")
    @PermissionScope(names = {"customer_update_mobile_number"})
    RestResult updateModileNumber(@PathVariable("id") Long id, @Valid @RequestBody UpdateMobileNumberWrapper phoneWrapper) {
        return customerService.updateModileNumber(id, phoneWrapper);
    }

    @DeleteMapping("/{id}/cache")
    @ApiOperation(value = "清除用户缓存", notes = "清除用户缓存,权限:customer_delete_cache")
    @PermissionScope(names = {"customer_delete_cache"})
    RestResult clearCache(@PathVariable("id") Long id) {
        return customerService.clearCache(id);
    }

    @PatchMapping("/{id}/is_freezed")
    @ApiOperation(value = "解封号", notes = "解封号,权限:customer_set_freezed")
    @PermissionScope(names = {"customer_set_freezed"})
    RestResult updateUnlock(@PathVariable("id") Long id,
                            @RequestBody UpdateUnlockWrapper updateUnlockWrapper) {
        return customerService.updateUnlock(id, updateUnlockWrapper);
    }

    @PostMapping("query_by_query")
    @ApiOperation(value = "查询用户信息(Excel)", notes = "查询用户信息通过excel，使用模板文件：QueryCustomer.xls， 权限：customer_give_coupon")
    @PermissionScope(names = {"customer_give_coupon"})
    RestResult<CustomerQueryResult> queryCustomerByExcel(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException {
        return customerService.queryCustomerByExcel(file);
    }

    @PostMapping("get_push_token")
    @ApiOperation(value = "查询用户的 push token", notes = "查询用户的 push token，使用模板文件：QueryCustomer.xls，返回文件流， 权限：get_customer_push_token")
    @PermissionScope(names = {"get_customer_push_token"})
    public void downloadPushToken(@RequestParam(value = "file", required = true) MultipartFile file, HttpServletResponse response) throws IOException {
        customerService.downloadPushToken(response, file);
    }

    @GetMapping("arrears")
    @ApiOperation(value = "欠款乘客列表", notes = "欠款乘客列表,权限:customer_arrears_list")
    @PermissionScope(names = {"customer_arrears_list"})
    RestResult<Page<CustomerArrearsListPresenter>> getArrearsList(@ModelAttribute("admin") AdminUser adminUser, CustomerArrearsQueryWrapper wrapper) {
        return customerService.getArrearsList(adminUser, wrapper);
    }

    @GetMapping("arrears/export")
    @ApiOperation(value = "欠款乘客导出", notes = "欠款乘客列表,权限:customer_arrears_export")
    @PermissionScope(names = {"customer_arrears_export"})
    void exportArrearsList(@ModelAttribute("admin") AdminUser adminUser, @Valid CustomerArrearsExportWrapper wrapper, HttpServletResponse response) throws IOException {
        customerService.exportArrearsList(adminUser, wrapper, response);
    }

    @GetMapping("/query_like")
    @ApiOperation(value = "乘客列表模糊查询", notes = "无权限")
    RestResult<List<CustomerQueryLikeListPresenter>> index(@Valid CustomerLikeQueryWrapper wrapper) {
        return customerService.getListLike(wrapper);
    }

}
